package me.bcloud.web.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import me.bcloud.web.dao.JobConfigDao;
import me.bcloud.web.dao.UserDao;
import me.bcloud.web.domain.JobConfig;
import me.bcloud.web.domain.User;
import me.bcloud.web.domain.WorkUser;
import me.bcloud.web.spring.Cache;

@Service
public class JobConfigService {

	@Resource(name = "jobConfigDaoMysqlJdbcImpl")
	private JobConfigDao jobConfigDao;

	@Resource
	private UserDao userDao;

	@Resource
	private Cache cache;

	public void setJobConfigDao(JobConfigDao jobConfigDao) {
		this.jobConfigDao = jobConfigDao;
	}

	@PostConstruct
	public void init() {

		
		
		List<JobConfig> jobConfigs = jobConfigDao.findAll();
	
		
		for (JobConfig jobConfig : jobConfigs) {
			System.out.println(jobConfig.getId());
			cache.getJobMap().put(jobConfig.getId(), jobConfig);
		
		}
		
		List<JobConfig> openJobs=getOpenJobs(jobConfigs);

		for (JobConfig jobConfig : openJobs) {
			System.out.println("put");
			cache.getCacheMap().put(jobConfig.getId(), new LinkedList<User>(userDao.findAll(jobConfig)));
			cache.getWorkMap().put(jobConfig.getId(), new HashMap<Integer,WorkUser>());
		}

	}

	private List<JobConfig> getOpenJobs( List<JobConfig>  jobConfigs) {
		List<JobConfig> openJobs=new ArrayList<JobConfig>();
		for (int i = 0; i < jobConfigs.size(); i++) {
			if (jobConfigs.get(i).isOpen()) {
				openJobs.add(jobConfigs.get(i));
			}

		}
		return openJobs;
	}
	
	public JobConfig getJob() {
		Random random = new Random();
		List<JobConfig> jobConfigs =getOpenJobs(new ArrayList<JobConfig>(cache.getJobMap().values()));
		if (jobConfigs.size() == 0) {
			return null;
		}
		int index = random.nextInt(jobConfigs.size());
		return jobConfigs.get(index);
	}
	
	
	public void open(boolean isopen,int id) {
		JobConfig jobConfig=cache.getJobMap().get(id);
		
		if (isopen) {
			
			cache.getCacheMap().put(id, new LinkedList<User>(userDao.findAll(jobConfig)));
			cache.getWorkMap().put(id, new HashMap<Integer,WorkUser>());

		}
		jobConfig.setOpen(isopen);
		jobConfigDao.update(jobConfig);

	}		
	
	public void edit(JobConfig newjobConfig) {
		
		jobConfigDao.update(newjobConfig);
		
		cache.getJobMap().put(newjobConfig.getId(), newjobConfig);
	}
	
	
	public void insert(JobConfig newjobConfig) {
		//建表
		userDao.create(newjobConfig.getWriteTable());
		jobConfigDao.insert(newjobConfig);
		
	}
	public void insert(JobConfig newjobConfig,String[] others) {
		//建表
		userDao.create(newjobConfig.getWriteTable(),others);
		jobConfigDao.insert(newjobConfig);
		
	}
	public boolean del(int jobId) {
		if (cache.getJobMap().get(jobId).isOpen()) {
			return false;
		}
		jobConfigDao.delete(jobId);
		return true;
	}

	public List<JobConfig> list() {
		
		List<JobConfig> jobConfigs= jobConfigDao.findAll();
		
		for (JobConfig jobConfig : jobConfigs) {
			if (!cache.getJobMap().containsKey(jobConfig.getId())) {
				cache.getJobMap().put(jobConfig.getId(),jobConfig);
			}
		}
		return jobConfigs;
	}
	
	public String getLastTime(String table) {
		return userDao.getLastTime(table);
	}
	
	public int getCount(String table) {
		return userDao.getMaxNum(table);
	}
	
	public int getDayOfCount(String table) {
		return userDao.getOneDayCount(table);
	}

	public void setCache(Cache cache) {
		this.cache = cache;
	}

}
